package com.snowfield.lb47.lbconfig.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.snowfield.lb47.lbconfig.domain.HttpCheckParam;
import com.snowfield.lb47.lbconfig.service.IHttpCheckParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * HTTP检测参数Controller
 *
 * @author snowfield
 * @date 2024-05-28
 */
@RestController
@RequestMapping("/lbconfig/checkparam")
public class HttpCheckParamController extends BaseController {
    @Autowired
    private IHttpCheckParamService httpCheckParamService;

    /**
     * 查询HTTP检测参数列表
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:checkparam:list')")
    @GetMapping("/list")
    public TableDataInfo list(HttpCheckParam httpCheckParam) {
        startPage();
        List<HttpCheckParam> list = httpCheckParamService.selectHttpCheckParamList(httpCheckParam);
        return getDataTable(list);
    }

    /**
     * 导出HTTP检测参数列表
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:checkparam:export')")
    @Log(title = "HTTP检测参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HttpCheckParam httpCheckParam) {
        List<HttpCheckParam> list = httpCheckParamService.selectHttpCheckParamList(httpCheckParam);
        ExcelUtil<HttpCheckParam> util = new ExcelUtil<HttpCheckParam>(HttpCheckParam.class);
        util.exportExcel(response, list, "HTTP检测参数数据");
    }

    /**
     * 获取HTTP检测参数详细信息
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:checkparam:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(httpCheckParamService.selectHttpCheckParamById(id));
    }

    /**
     * 新增HTTP检测参数
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:checkparam:add')")
    @Log(title = "HTTP检测参数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HttpCheckParam httpCheckParam) {
        if (!httpCheckParamService.checkNameUnique(httpCheckParam)) {
            return AjaxResult.error("新增检测参数 '" + httpCheckParam.getParamName() + "'失败，名称已经存在");
        }
        return toAjax(httpCheckParamService.insertHttpCheckParam(httpCheckParam));
    }

    /**
     * 修改HTTP检测参数
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:checkparam:edit')")
    @Log(title = "HTTP检测参数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HttpCheckParam httpCheckParam) {
        if (!httpCheckParamService.checkNameUnique(httpCheckParam)) {
            return AjaxResult.error("修改检测参数 '" + httpCheckParam.getParamName() + "'失败，名称已经存在");
        }
        return toAjax(httpCheckParamService.updateHttpCheckParam(httpCheckParam));
    }

    /**
     * 删除HTTP检测参数
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:checkparam:remove')")
    @Log(title = "HTTP检测参数", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(httpCheckParamService.deleteHttpCheckParamByIds(ids));
    }
}
